package com.ling.pl.security.view.frame.main;

import com.bstek.dorado.common.event.ClientEvent;
import com.bstek.dorado.common.event.DefaultClientEvent;
import com.bstek.dorado.view.View;
import com.bstek.dorado.view.widget.base.menu.MenuItem;
import com.bstek.dorado.view.widget.base.toolbar.MenuButton;
import com.bstek.dorado.view.widget.base.toolbar.ToolBar;
import com.ling.pl.core.commons.utils.AssertHelper;
import com.ling.pl.security.model.DefaultUrlVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;

@Component("mainFrameMenuListener")
public class MainFrameMenuListener {
    @Resource
    MainFrame mainFrame;

    public void onViewInit(View view) {
        ToolBar toolBar = (ToolBar) view.getViewElement("toolBarNav");
        if (toolBar != null) {
            Collection<DefaultUrlVO> urls = mainFrame.loadMeunUrls(null);
            processMenu(toolBar, urls);
        }
    }

    private void processMenu(Object parent, Collection<DefaultUrlVO> urls) {
        for (DefaultUrlVO url : urls) {
            if (parent instanceof ToolBar) {
                MenuButton menuButton = processUrlToMenuButton(url);
                ToolBar toolBar = (ToolBar) parent;
                toolBar.addItem(menuButton);
                processMenu(menuButton, url.getChildren());
            }
            if (parent instanceof MenuButton) {
                MenuItem menuItem = convertUrlToMenuItem(url);
                MenuButton menuButton = (MenuButton) parent;
                menuButton.addItem(menuItem);
                processMenu(menuItem, url.getChildren());
            }
            if (parent instanceof MenuItem) {
                MenuItem menuItem = convertUrlToMenuItem(url);
                MenuItem parentMenuItem = (MenuItem) parent;
                parentMenuItem.addItem(menuItem);
                processMenu(menuItem, url.getChildren());
            }
        }
    }

    private MenuButton processUrlToMenuButton(DefaultUrlVO url) {
        MenuButton menuButton = new MenuButton();
        menuButton.setCaption(url.getName());
        if (AssertHelper.notEmpty(url.getIcon())) {
            menuButton.setIcon(url.getIcon());
        }
        if (AssertHelper.notEmpty(url.getUrl())) {
            ClientEvent clientEvent = new DefaultClientEvent("window.openUrlInFrameTab('" + url.getUrl() + "','" + url.getName() + "','" + url.getIcon() + "',true,true);");
            menuButton.addClientEventListener("onClick", clientEvent);
        }
        return menuButton;
    }

    private MenuItem convertUrlToMenuItem(DefaultUrlVO url) {
        MenuItem menuItem = new MenuItem();
        menuItem.setCaption(url.getName());
        if (AssertHelper.notEmpty(url.getIcon())) {
            menuItem.setIcon(url.getIcon());
        }
        if (AssertHelper.notEmpty(url.getUrl())) {
            ClientEvent clientEvent = new DefaultClientEvent("window.openUrlInFrameTab('" + url.getUrl() + "','" + url.getName() + "','" + url.getIcon() + "',true,true);");
            menuItem.addClientEventListener("onClick", clientEvent);
        }
        return menuItem;
    }
}
